package com.module.common.base.resource;

import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.*;

import com.module.common.base.entity.MpBaseEntity;
import com.module.common.base.service.MpCrudService;

public class MpCrudResource<Data extends MpBaseEntity, Service extends MpCrudService<Data>> {

	protected Service service;

	public MpCrudResource(Service service) {
		this.service = service;
	}

	@GetMapping("/{id}")
	public Data findById(@PathVariable Long id) {
		Optional<Data> userOptional = service.findOne(id);
		return userOptional.orElse(null);
	}

	@GetMapping("/unArchived/{id}")
	public Data findUnArchived(@PathVariable Long id) {
		Optional<Data> data = service.findOne(id);
		return data.orElse(null);
	}

	@GetMapping("/ids")
	public List<Data> findByIds(@RequestParam("ids") List<Long> ids) {
		return service.findByIds(ids);
	}

	@GetMapping("/all")
	public List<Data> findAll() {
		return service.findAll();
	}

	@PostMapping
	public Data create(@RequestBody Data data) {
		return service.create(data);
	}

	@PostMapping("/batch/create")
	public List<Data> batchCreate(@RequestBody List<Data> datas) {
		return service.batchCreate(datas);
	}

	@PutMapping
	public void update(@RequestBody Data data) {
		service.update(data);
	}

	@PutMapping("/all")
	public void updateAllProperties(@RequestBody Data data) {
		service.updateAllProperties(data);
	}

	@DeleteMapping("/{id}")
	public void delete(@PathVariable Long id) {
		service.delete(id);
	}

	@PutMapping("/achieve/{id}")
	public void achieve(@PathVariable Long id) {
		service.achieve(id);
	}
}
